$(window).load(function() {
    var linksObj = new objEven(".loginInfo .down");
    linksObj.delayHover();

    var areaBox = new box();
    $(".areaTitle .slide").click(function() {
        areaBox.slideClick($(this), "area");
    });
    var tipsBox = new box();
    $(".tipsError .tipsSlide").live("click",function() {
        tipsBox.slideClick($(this), "tips", "Error");
    });
    $(".tipsInfo .tipsSlide").click(function() {
        tipsBox.slideClick($(this), "tips", "Info");
    });
    $(".tipsSuccess .tipsSlide").click(function() {
        tipsBox.slideClick($(this), "tips", "Success");
    });
    $(".tipsWarning .tipsSlide").click(function() {
        tipsBox.slideClick($(this), "tips", "Warning");
    });
    $(".tipsClose").live("click",function() {
        tipsBox.closeClick($(this), "tips");
    });

    var QAsidebar = new SideBar();
    $("#SideBar .sideBarTop .slide").toggle(
			function() { QAsidebar.slideOff(); },
			function() { QAsidebar.slideOn(); }
		);
    $(".menuTitle .slide").toggle(
			function() { QAsidebar.menuOn($(this)); },
			function() { QAsidebar.menuOff($(this)); }
		);
    $(".subMenu li a").hover(
			function() { QAsidebar.subMenuOver($(this)); },
			function() { QAsidebar.subMenuOut($(this)); }
		).click(function() {
		    QAsidebar.subMenuCurrent($(this));
		});
    $(".menu").hover(
			function() { QAsidebar.menuOver($(this)); },
			function() { QAsidebar.menuOut($(this)); }
		)
    $("span.menuItem").click(function() { $(this).next(".slide").click() });

    FormWidget.dft("");
    FormWidget.setDisableAll("");
});
var SideBar = function() {
    var self = this;
    var timer1, timer2;
    self.state = "on";
    self.slideOn = function() {
        $("#Body").removeClass("sideBarOff");
        self.state = "on";
    }
    self.slideOff = function() {
        $("#Body").addClass("sideBarOff");
        self.state = "off";
    }
    self.menuOn = function(o) {
        if (self.state == "on") {
            var pObj = o.parents(".menu");
            if ($(".menuBody", pObj).length) {
                pObj.addClass("menu_on");
            }
        }
    }
    self.menuOff = function(o) {
        if (self.state == "on") {
            var pObj = o.parents(".menu");
            if ($(".menuBody", pObj).length) {
                pObj.removeClass("menu_on");
            }
        }
    }
    self.menuOver = function(o) {
        if (self.state == "off" && $(".menuBody", o).length) {
            if (o.data("cur") != 1) {
                timer1 = setTimeout(function() {
                    o.addClass("menu_hover").data("cur", 1);
                }, 350);
            } else {
                if (timer2) clearTimeout(timer2);
            }
        }
    }
    self.menuOut = function(o) {
        if (self.state == "off" && $(".menuBody", o).length && timer1) {
            clearTimeout(timer1);
            if (o.data("cur") == 1) {
                timer2 = setTimeout(function() {
                    o.removeClass("menu_hover").data("cur", 0);
                }, 350);
            }
        }
    }
    self.subMenuOver = function(o) {
        o.parent().addClass("hover");
    }
    self.subMenuOut = function(o) {
        o.parent().removeClass("hover");
    }
    self.subMenuCurrent = function(o) {
        $(".menu").removeClass("menu_current");
        $(".subMenu li").removeClass("current");
        o.parents(".menu").addClass("menu_current");
        o.parent().addClass("current");
    }
}
var objEven = function(o) {
    var obj = $(o);
    this.hover = function() {
        obj.hover(
			function() { $(this).addClass('hover') },
			function() { $(this).removeClass('hover') }
		);
    }
    this.delayHover = function() {
        var timer1, timer2;
        obj.hover(
			function() {
			    var self = $(this);
			    if (self.data("cur") != 1) {
			        timer1 = setTimeout(function() {
			        //self.addClass('hover').data("cur", 1);
			        self.addClass('hover').data("cur", 1).css("position", "relative");
			        }, 350);
			    } else {
			        if (timer2) clearTimeout(timer2);
			    }
			},
			function() {
			    if (timer1) {
			        var self = $(this);
			        clearTimeout(timer1);
			        if (self.data("cur") == 1) {
			            timer2 = setTimeout(function() {
			            //self.removeClass('hover').data("cur", 0);
			            self.removeClass('hover').data("cur", 0).css("position", "static");
			            }, 350);
			        }
			    }
			}
		);
    }
    this.current = function() {
        obj.click(function() {
            var self = $(this);
            self.siblings().removeClass("current").end().addClass("current");
        }).mouseover(function() {
            $(this).click();
        });
    }
    this.tabs = function(objID, objClass, dftNum) {
        var oID = "#" + objID;
        var oClass = "." + objClass;
        var curNum = dftNum;
        obj.click(function() {
            var self = $(this);
            curNum = self.attr("rel");
            self.siblings().removeClass("current").end().addClass("current");
            $("[id^='" + objID + "_']").hide();
            $("#" + objID + "_" + curNum).show();
            return false;
        });
        $(oID + " " + oClass + "Prev").click(function() {
            curNum = parseInt(curNum) - 1;
            if (curNum < 1) curNum = obj.length;
            obj.eq(curNum - 1).click();
            return false;
        });
        $(oID + " " + oClass + "Next").click(function() {
            curNum = parseInt(curNum) + 1;
            if (curNum > obj.length) curNum = 1;
            obj.eq(curNum - 1).click();
            return false;
        });
        obj.eq(dftNum - 1).click();
    }
}

var box = function() {
    var self = this;
    self.slideClick = function(o, s, type) {
        if (!type) type = "";
        var tBox = o.parents("." + s);
        var tTitle = o.parents("." + s + "Title");
        var tBody = tTitle.next("." + s + "Body");
        if (tBody.css("display") == "block") {
            tBox.addClass(s + type + "Off");
            tBody.hide();
        } else {
            tBox.removeClass(s + type + "Off");
            tBody.show();
        }
    }
    self.closeClick = function(o, s) {
        o.parents("." + s).hide();
    }
}

var FormWidget = {
    inputBox: '<div class="inputBox"><div class="outer"><div class="inner"></div></div></div>',
    textareaBox: '<div class="textareaBox"><div class="outer"><div class="inner"></div></div></div>',
    selectBox: '<div class="selectBox"></div>',
    selectBoxStyle: '<div class="selectBoxInput"><div class="outer"> \
							<input type="text" class="text" value="" /> \
							<span class="dropdown"></span> \
						</div></div> \
						<div class="selectBoxList selectFree"> \
							<div class="selectBoxListTop"><span class="circleL"></span><span class="circleR"></span></div> \
							<ul class="selectBoxListBody"></ul> \
							<div class="selectBoxListBottom"><span class="circleL"></span><span class="circleR"></span></div> \
							<!--[if lte IE 6.5]><iframe></iframe><![endif]--> \
						</div> ',
    dft: function(area) {
        var objArea;
        if (area != "") objArea = $(area);
        var timer1, timer2;
       
        $(".addUserList", objArea).wrap(FormWidget.textareaBox);
        $(".select", objArea)
			.wrap(FormWidget.selectBox)
        	.parent().prepend(FormWidget.selectBoxStyle)
        			.find(".select option")
        				.each(function(index, domEle) {
        				    if (index == 0 || domEle.selected) {
        				        $(domEle).parents(".selectBox").find(".selectBoxInput .text").val($(domEle).text());
        				    }
        				    //$(domEle).parents(".selectBox").find(".selectBoxListBody").append('<li optionValue="' + domEle.value + '"><a href="javascript:void(0)">' + $(domEle).text() + '</a></li>');
        				    $(domEle).parents(".selectBox").find(".selectBoxListBody").append('<li style="font-size:0.95em" optionValue="' + $(domEle).val() + '"><a href="javascript:void(0)">' + $(domEle).text() + '</a></li>');
        				});
        $(".selectBoxInput .text", objArea)
			.each(function(index, domEle) {
			    var width = $(domEle).parents(".selectBox").find(".select").width();
			    $(domEle).width(width - 23);
			    $(domEle).parents(".selectBox").find(".selectBoxList").width(width + 2);
			    MyKeyboard.ini(domEle);
			});
        $(".selectBoxInput .dropdown", objArea)
			.click(function() {


			    var self = $(this);
			    if (self.parent().find("input").attr("disabled") == true)
			        return;



			    self.parents(".selectBox")
						.addClass('selectBoxHover')
						.data("cur", 1)
						.css({ "z-index": "2", "position": "relative" })
						.find(".selectBoxInput .text").focus();
			});
        $(".selectBox", objArea).each(function() {
            if ($(this).find("select:disabled").length == 0) {
                $(this).hover(function() {
                    var self = $(this);
                    if (self.data("cur") != 1) {
                        timer1 = setTimeout(function() {
                            self.addClass('selectBoxHover').data("cur", 1);
                            self.css({ "z-index": "2", "position": "relative" });
                        }, 100);
                    } else {
                        if (timer2) clearTimeout(timer2);
                    }
                },
				function() {
				    if (timer1) {
				        var self = $(this);
				        clearTimeout(timer1);
				        if (self.data("cur") == 1) {
				            timer2 = setTimeout(function() {
				                self.removeClass('selectBoxHover').data("cur", 0);
				                self.css({ "z-index": "1", "position": "static" });
				            }, 100);
				        }
				    }
				}
			);
            }
            else {
                $(this).find(":text").attr("disabled", true);
            }
        });

        $(".selectBoxListBody li", objArea)
			.click(function() {
			    FormWidget.setSelected($(this));
			    $(this).parents(".selectBox").mouseout();
			    return false;
			});
        $(".input", objArea)
			.wrap(FormWidget.inputBox)
			.focus(function() {
			    //if (this.value == this.defaultValue) this.value = "";
			    $(this).parents(".inputBox").addClass("inputBoxFocus");
			    this.select();
			}).blur(function() {
			    //if (this.value == "") this.value = this.defaultValue;
			    $(this).parents(".inputBox").removeClass("inputBoxFocus");
			});
        $(".textarea", objArea)
			.wrap(FormWidget.textareaBox)
			.focus(function() {
			    $(this).parents(".textareaBox").addClass("textareaBoxFocus");
			}).blur(function() {
			    $(this).parents(".textareaBox").removeClass("textareaBoxFocus");
			});
    },
    setSelected: function(obj) {
        var box = obj.parents(".selectBox");
        box.find(".selectBoxInput .text").val(obj.text());
        box.find("li").removeClass("hover");
        obj.addClass("hover");

        box.find(".select option")
			.each(function(index, domEle) {
			    if (isIE()) {
			        //if (self.attr("optionValue") == domEle.value) {
			        if (obj.attr("optionValue") == $(domEle).val()) { /*------2010.11.11 tiya modify, domEle.value��ie7��ȡ����ֵ�����Ը�Ϊ $(domEle).val()����ע��---------*/
			            domEle.selected = true;
			            if (domEle.parentNode.onchange != null)
			                domEle.parentNode.onchange();
			        }
			    }
			    else {
			        //if (self.attr("optionValue") == domEle.value) {
			        if (obj.attr("optionValue") == $(domEle).val()) { /*------2010.11.11 tiya modify, domEle.value��ie7��ȡ����ֵ�����Ը�Ϊ $(domEle).val()����ע��---------*/
			            domEle.selected = true; //domEle.parentNode.onchange();
			            if (domEle.parentNode.onchange != null)
			                domEle.parentNode.onchange();
			        }
			    }
			});
    },
    setError: function(area) {
        $(area + " input.setError").parents(".inputBox").addClass("inputBoxError");
    },
    setSucceed: function(area) {
        $(area + " input.setSucceed").parents(".inputBox").removeClass("inputBoxError").addClass("inputBoxSucceed");
    },
    setDisableAll: function(area) {
        $(area + " input:disabled").parents(".inputBox").addClass("inputBoxDisable");
    },
    setDisable: function(obj) {
        $(obj).attr("disabled", "disabled").parents(".inputBox").addClass("inputBoxDisable");
    },
    setEnable: function(obj) {
        $(obj).removeAttr("disabled").parents(".inputBox").removeClass("inputBoxDisable");
    }
}

var MyFun = {
    grid: function(o) {
        var obj = $(o);
        $(o + ":odd").addClass("even");
        $(obj).hover(
		   function() { $(this).addClass("hover"); },
		   function() { $(this).removeClass("hover"); }
		).click(function() {
		    $(obj).removeClass("current");
		    $(this).addClass("current");
		});
    },
    cloneTemplate: function(o, t) {
        var obj = $(o);
        var temp = $(t);
        obj.append(temp);
        FormWidget.dft(temp);
    }
}

var MyKeyboard = {
    ini: function(obj) {
        var textKeyNum = 0;
        var objSearchText = $(obj);
        var objSearchList = objSearchText.parents(".selectBox").find(".selectBoxListBody").eq(0);
        var objSearchList_li = $("li", objSearchList);
        var key = "";
        var timer1;

        objSearchText.keyup(function(event) {
            switch (event.keyCode) {
                case 13:
                    objSearchList_li.eq(textKeyNum).click();
                    break;
                case 38:
                    textKeyNum--;
                    if (textKeyNum < 0) {
                        textKeyNum = objSearchList_li.length - 1;
                    }
                    break;
                case 40:
                    textKeyNum++;
                    if (textKeyNum > objSearchList_li.length - 1) {
                        textKeyNum = 0;
                    }
                    break;

                default:
                    if (timer1) clearTimeout(timer1);
                    key = key + String.fromCharCode(event.keyCode).toLowerCase();
                    var keyLen = key.length;
                    objSearchList_li.each(function(index, domEle) {
                        if ($(domEle).text().substring(0, keyLen).toLowerCase() == key) {
                            textKeyNum = index;
                            FormWidget.setSelected($(domEle));
                            return false;
                        }
                    });
                    timer1 = setTimeout(function() {
                        key = "";
                    }, 500);
                    break;
            }
            MyKeyboard.curSelect(textKeyNum, objSearchList, objSearchList_li);
        }).focus(function() {
            objSearchList_li.each(function(index, domEle) {
                if ($(domEle).text() == objSearchText.val()) {
                    textKeyNum = index;
                    FormWidget.setSelected($(domEle));
                    return false;
                }
            });
            MyKeyboard.curSelect(textKeyNum, objSearchList, objSearchList_li);
        });
    },
    curSelect: function(n, list, li) {
        li.removeClass("hover").eq(n).addClass("hover");
        FormWidget.setSelected(li.eq(n));
        if (n >= 3) {
            list.get(0).scrollTop = li.eq(0).height() * (n - 2);
        } else {
            list.get(0).scrollTop = 0;
        }
    }
}

function showAdvanced(type) {
    if (type == "hide") $(".form tr.advSearch").hide();
    $(".form caption .normalSearch").click(function() {
        $(this).parent().find("span").removeClass("current");
        $(this).addClass("current").parents(".form").find("tr.advSearch").hide();
    });
    $(".form caption .advSearch").click(function() {
        $(this).parent().find("span").removeClass("current");
        $(this).addClass("current").parents(".form").find("tr.advSearch").show();
    });
}